//
//  UnionListViewController.m
//  PlayBall
//
//  Created by yanfa on 16/4/20.
//
//

#import "UnionListViewController.h"
#import "SetYongJinViewController.h"


@interface UnionListViewController ()
{
    int tabIndex;
    int selectLotIndex;
}

@end

@implementation UnionListViewController{
    
}

SHARED_VIEW_CONTROLLER_IMPLEMENT(UnionListViewController);




- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:@"我的会员"];
    customLab.textAlignment = NSTextAlignmentCenter;
    
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    //self.navigationItem.hidesBackButton = YES;
    
    _btn_lot_name.layer.borderWidth = 1;
    _btn_lot_name.layer.borderColor = [UIColor darkGrayColor].CGColor;
    _btn_lot_name.layer.cornerRadius = 3;
    _btn_lot_name.clipsToBounds = YES;
    
    _btn_end_date.layer.borderWidth = 1;
    _btn_end_date.layer.borderColor = [UIColor darkGrayColor].CGColor;
    _btn_end_date.layer.cornerRadius = 2;
    _btn_end_date.clipsToBounds = YES;
    
    _btn_start_date.layer.borderWidth = 1;
    _btn_start_date.layer.borderColor = [UIColor darkGrayColor].CGColor;
    _btn_start_date.layer.cornerRadius = 2;
    _btn_start_date.clipsToBounds = YES;
    
    _tf_username.layer.borderWidth = 1;
    _tf_username.layer.borderColor = [UIColor darkGrayColor].CGColor;
    _tf_username.layer.cornerRadius = 3;
    _tf_username.clipsToBounds = YES;
    
    tabIndex = 0;
    selectLotIndex = 0;
    [_segTab setSelectedSegmentIndex:tabIndex];
//    [self getDatebyTabIndex:tabIndex];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getDatebyTabIndex:tabIndex];
}

-(void)getDatebyTabIndex:(int) tabIndex{
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *  currDate=[NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    if(tabIndex == 0){
        [_label_lot setHidden:YES];
        [_btn_lot_name setHidden:YES];
        [_view_myunions setHidden:NO];
        [_view_total setHidden:YES];
        [_view_detail setHidden:YES];
        [_label_no_data_tips setHidden:YES];
        _labe_end_des.text = @"";
        NSDateComponents *adcomps = [[NSDateComponents alloc] init];
        [adcomps setYear:0];
        [adcomps setMonth:-3];
        [adcomps setDay:0];
        NSDate *startdate = [calendar dateByAddingComponents:adcomps toDate:currDate options:0];
        
        currSearchDate=[dateformatter stringFromDate:currDate];
        startDateStr = [dateformatter stringFromDate:startdate];
        [_btn_end_date setTitle:currSearchDate forState:UIControlStateNormal];
        [_btn_start_date setTitle:startDateStr forState:UIControlStateNormal];
        [_btn_end_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_btn_start_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
       
    }else if(tabIndex == 1){
        [_label_lot setHidden:YES];
        [_btn_lot_name setHidden:YES];
        [_view_myunions setHidden:YES];
        [_view_total setHidden:NO];
        [_view_detail setHidden:YES];
        _labe_end_des.text = @"";
        [_label_no_data_tips setHidden:YES];
        NSDateComponents *adcomps = [[NSDateComponents alloc] init];
        [adcomps setYear:0];
        [adcomps setMonth:0];
        [adcomps setDay:-6];
        NSDate *startdate = [calendar dateByAddingComponents:adcomps toDate:currDate options:0];
        currSearchDate=[dateformatter stringFromDate:currDate];
        startDateStr = [dateformatter stringFromDate:startdate];
        [_btn_end_date setTitle:currSearchDate forState:UIControlStateNormal];
        [_btn_start_date setTitle:startDateStr forState:UIControlStateNormal];
        [_btn_end_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_btn_start_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }else if(tabIndex == 2){
        [_label_lot setHidden:NO];
        [_btn_lot_name setHidden:NO];
        [_view_myunions setHidden:YES];
        [_view_total setHidden:YES];
        [_view_detail setHidden:NO];
        [_label_no_data_tips setHidden:YES];
        selectLotIndex = 0;
        selectLotId = @"0";
        _labe_end_des.text = @"";
        NSDateComponents *adcomps = [[NSDateComponents alloc] init];
        [adcomps setYear:0];
        [adcomps setMonth:0];
        [adcomps setDay:-6];
        NSDate *startdate = [calendar dateByAddingComponents:adcomps toDate:currDate options:0];
        currSearchDate=[dateformatter stringFromDate:currDate];
        startDateStr = [dateformatter stringFromDate:startdate];
        [_btn_end_date setTitle:currSearchDate forState:UIControlStateNormal];
        [_btn_start_date setTitle:startDateStr forState:UIControlStateNormal];
        [_btn_end_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_btn_start_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [_btn_lot_name setTitle:@"全部" forState:UIControlStateNormal];
        [_btn_lot_name setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }
    [self requestData];
}

-(void)requestData{
    [dataUnions removeAllObjects];
    [_table_list reloadData];
    if(tabIndex == 0){
        GetUnionList *operation = [[GetUnionList alloc] init];
        operation.userName = _tf_username.text;//[JddUserProfile sharedInstance].name;
        operation.startDate= startDateStr;
        operation.endDate = currSearchDate;
        operation.delegate = self;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }else if(tabIndex == 1){
        GetJiaoYITotal *operation = [[GetJiaoYITotal alloc] init];
        operation.userName = _tf_username.text;//[JddUserProfile sharedInstance].name;
        operation.startDate= startDateStr;
        operation.endDate = currSearchDate;
        operation.delegate = self;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }else if(tabIndex == 2){
        GetJiaoYiDetail *operation = [[GetJiaoYiDetail alloc] init];
        operation.userName = _tf_username.text;//[JddUserProfile sharedInstance].name;
        operation.startDate= startDateStr;
        operation.endDate = currSearchDate;
        operation.delegate = self;
        operation.lotteryid = selectLotId;
        [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)GetUnionListResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        SBJsonParser *jsonParse = [[SBJsonParser alloc] init];
        NSString *realresult = [JddDataParser parseBuyHistoryDetailResult:result];
            NSDictionary *dataDic = [jsonParse objectWithString:realresult];
            dataUnions = [dataDic objectForKey:@"Union"];
            _labe_end_des.text = [NSString stringWithFormat:@"总下级：%@",[dataDic objectForKey:@"Total"]];
        if ([dataUnions count] < 1) {
            [_label_no_data_tips setHidden:NO];
        }else{
            [_label_no_data_tips setHidden:YES];
        }
        [_table_list reloadData];
    }
}

-(void)GetJiaoYITotalResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        SBJsonParser *jsonParse = [[SBJsonParser alloc] init];
        NSString *realresult = [JddDataParser parseBuyHistoryDetailResult:result];
        NSDictionary *dataDic = [jsonParse objectWithString:realresult];
        dataUnions = [dataDic objectForKey:@"Union"];
        _labe_end_des.text = [NSString stringWithFormat:@"总销量：%@彩豆      总佣金：%@彩豆",[dataDic objectForKey:@"Money"],[dataDic objectForKey:@"Bouns"]];
        if ([dataUnions count] < 1) {
            [_label_no_data_tips setHidden:NO];
        }else{
            [_label_no_data_tips setHidden:YES];
        }
        [_table_list reloadData];
    }
}

-(void)GetJiaoYiDetailResult:(NSData *)result{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        SBJsonParser *jsonParse = [[SBJsonParser alloc] init];
        NSString *realresult = [JddDataParser parseBuyHistoryDetailResult:result];
        NSDictionary *dataDic = [jsonParse objectWithString:realresult];
        dataUnions = [dataDic objectForKey:@"Union"];
        _labe_end_des.text = [NSString stringWithFormat:@"认购总金额：%@彩豆      总佣金：%@彩豆",[dataDic objectForKey:@"Money"],[dataDic objectForKey:@"Bouns"]];
        if ([dataUnions count] < 1) {
            [_label_no_data_tips setHidden:NO];
        }else{
            [_label_no_data_tips setHidden:YES];
        }
        [_table_list reloadData];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [_tf_username resignFirstResponder];
    return YES;
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_tf_username resignFirstResponder];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_tf_username resignFirstResponder];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tabIndex == 0){
        UnionListTableViewCell*cell = [UnionListTableViewCell getInstanceView];
        NSDictionary* item = [dataUnions objectAtIndex:indexPath.row];
        cell.label_username.text= [item objectForKey:@"UserName"];
        cell.startdate.text = [item objectForKey:@"DateTime"];
        cell.label_unions.text = [NSString stringWithFormat:@"%@",[item objectForKey:@"SubCount"]];
        cell.label_isdali.text = [item objectForKey:@"IsUion"];
        return cell;
    }else if (tabIndex == 1){
        TotalTableViewCell*cell = [TotalTableViewCell getInstanceView];
        NSDictionary* item = [dataUnions objectAtIndex:indexPath.row];
        cell.label_userName.text= [item objectForKey:@"UserName"];
        cell.label_xiaoLiang.text = [item objectForKey:@"Money"];
        cell.label_yongjin.text = [item objectForKey:@"Bouns"];
        return cell;
    }else if (tabIndex == 2){
        DetailTableViewCell *cell = [DetailTableViewCell getInstanceView];
        NSDictionary* item = [dataUnions objectAtIndex:indexPath.row];
        cell.label_username.text = [item objectForKey:@"UserName"];
        cell.label_lotName.text = [item objectForKey:@"LottName"];
        cell.label_fanganMoney.text = [item objectForKey:@"Money"];
        cell.label_yongjin.text = [item objectForKey:@"Bouns"];
        cell.lebel_faqiTime.text = [[item objectForKey:@"DateTime"] stringByReplacingOccurrencesOfString:@" " withString:@"\n"];
        return cell;
    }else{
        return nil;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataUnions count];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tabIndex == 0){
        NSLog(@"%d",indexPath.row);
//        SetYongJinViewController *controller = [SetYongJinViewController sharedInstance];
        SetYongJinViewController *controller = [[SetYongJinViewController alloc] init];
        NSDictionary* item = [dataUnions objectAtIndex:indexPath.row];
        controller.subuserid = [item objectForKey:@"UserID"];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30.0f;
}



-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if([actionSheet isMemberOfClass:[MyGetDateView class]]){
        MyGetDateView *locateViewTemp = (MyGetDateView *)actionSheet;
        if(buttonIndex == 0) {
        }else {
            if(locateViewTemp.callByWhoId == 0){
                [_btn_start_date setTitle:locateViewTemp.selectDate forState:UIControlStateNormal];
                [_btn_start_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            }else{
                [_btn_end_date setTitle:locateViewTemp.selectDate forState:UIControlStateNormal];
                [_btn_end_date setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            }
        }
    }else if([actionSheet isMemberOfClass:[PCLocationView class]]){
        PCLocationView *locateViewTemp = (PCLocationView *)actionSheet;
        if(buttonIndex == 0) {
        }else {
            [_btn_lot_name setTitle:locateViewTemp.selectLotName forState:UIControlStateNormal];
            [_btn_lot_name setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            selectLotIndex = locateViewTemp.selectIndex;
            selectLotId = locateViewTemp.selectLotId;
        }
    }

}

- (IBAction)btnLotClick:(id)sender {
    getLotView = [[PCLocationView alloc] initWithTitle:@"选择彩种" delegate:self];
    [getLotView showInView:self.view withSelectIndex:selectLotIndex];
}

- (IBAction)tabSegSelectChanged:(id)sender {
    tabIndex = _segTab.selectedSegmentIndex;
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    [self getDatebyTabIndex:tabIndex];
}

- (IBAction)btnStratDateClick:(id)sender {
    getDateView = [[MyGetDateView alloc] initWithTitle:@"选择日期" delegate:self];
    getDateView.callByWhoId = 0;
    [getDateView showInView:self.view];
}

- (IBAction)btnEndDateClick:(id)sender {
    getDateView = [[MyGetDateView alloc] initWithTitle:@"选择日期" delegate:self];
    getDateView.callByWhoId = 1;
    [getDateView showInView:self.view];
}

- (IBAction)btnSearchClick:(id)sender {
    [_tf_username resignFirstResponder];
    [self requestData];
}
@end
